const app = getApp();
import { $wuxToast } from '../miniprogram_npm/wux-weapp/index.js'
function request(method,url,params) {
  return new Promise(function (resolve, reject) {
    wx.showLoading({
      title: '请求中...',
      mask:true
    })
     wx.request({
      url: app.globalData.baseUrl + url,
      data: params,
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/json'
      }, 
      success: function (res) {
        wx.hideLoading();
        if (res.statusCode == 200) { //网络请求状态
          if(res.data.code == 200){　//后端返回code ==200成功，其他失败
            resolve(res.data)
          }else{
            reject(res.data);
          }
        } else {
          $wuxToast().show({
            type: 'text',
            duration: 1500,
            color: '#fff',
            text: '请求出错',
            success: () => console.log('请求出错')
        })
        }
      },
      fail: function (e) {
        wx.hideLoading()
        $wuxToast().show({
          type: 'text',
          duration: 1500,
          color: '#fff',
          text: '请求失败',
          success: () => console.log('请求失败')
      })
      }
    })
  })
}


module.exports = request